

#include "Setting.h"
#include "assert.h"
#include "NumKeyBoard.h"
#include <qlistbox.h>
#include <qtextstream.h>
#include <qpushbutton.h>
#include <qtextcodec.h>


Setting::Setting( QWidget* parent , const char* name , bool modal , WFlags fl, int id ) : SettingDialog(parent, name, modal, fl), m_id(id)
{
	QString settingFileName[] = {(tr("/setting0.dat")),(tr("/setting1.dat")), (tr("/setting2.dat"))};
	m_settingFile = new QFile(settingFileName[m_id]);
	assert(m_settingFile);


	connect(pipeLenButton, SIGNAL(clicked()), this, SLOT(pipeLenButtonClicked()));
	connect(radiusButton, SIGNAL(clicked()), this, SLOT(radiusButtonClicked()));
	connect(maxBoardLenButton, SIGNAL(clicked()), this, SLOT(maxBoardLenButtonClicked()));
	connect(mixBoardLenButton, SIGNAL(clicked()), this, SLOT(mixBoardLenButtonClicked()));
	bool isOk = m_settingFile->open(IO_ReadWrite);
	assert(isOk);




}



Setting::~Setting()
{
	delete m_settingFile;
}


int Setting::loadSettingFile()
{
	bool isOk = false;
	QString str;
	m_code = QTextCodec::codecForName("Utf-8");

	QTextStream text(m_settingFile);




	str = text.readLine();
	m_pipeLen = str.toInt(&isOk, 10);
	assert(isOk);
	str.prepend(tr("流水线长度(mm):		"));
	str = m_code->toUnicode(str);
	pipeLenButton->setText(str);




	str = text.readLine();
	m_radius = str.toInt(&isOk,10);
	assert(isOk);
	str.prepend(tr("感应器转轮半径(mm):		"));
	str = m_code->toUnicode(str);
	radiusButton->setText(str);



	str = text.readLine();
	m_maxBoardLen = str.toInt(&isOk, 10);
	assert(isOk);
	str.prepend(tr("最大板长(mm):		"));
	str = m_code->toUnicode(str);
	maxBoardLenButton ->setText(str);




	str = text.readLine();
	m_mixBoardLen = str.toInt(&isOk, 10);;
	assert(isOk);
	str.prepend(tr("最小板长(mm):		"));
	str = m_code->toUnicode(str);
	mixBoardLenButton ->setText(str);



	str = tr("返回");
	str = m_code->toUnicode(str);
	backButton->setText(str);

	return 0;

}

int Setting::loadFile()
{
	return loadSettingFile();
}



void Setting::pipeLenButtonClicked()
{
	QString str;
	m_pipeLen = getNum();
	assert(m_pipeLen != -1);
	str.setNum(m_pipeLen);
	str.prepend(tr("流水线长度(mm):		"));
	str = m_code->toUnicode(str);
	pipeLenButton->setText(str);
}



void Setting::radiusButtonClicked()
{
	QString str;
	m_radius = getNum(); 
	assert(m_radius  != -1);
	str.setNum(m_radius) ;
	str.prepend(tr("感应器转轮半径(mm):		"));
	str = m_code->toUnicode(str);
	radiusButton->setText(str);

}
void Setting::maxBoardLenButtonClicked()
{
	QString str;
	m_maxBoardLen = getNum(); 
	assert(m_maxBoardLen  != -1);
	str.setNum(m_maxBoardLen );
	str.prepend(tr("最大板长(mm):		"));
	str = m_code->toUnicode(str);
	maxBoardLenButton->setText(str);


}
void Setting::mixBoardLenButtonClicked()
{
	QString str;
	m_mixBoardLen = getNum();
	assert(m_mixBoardLen != -1);
	str.setNum(m_mixBoardLen );

	str.prepend(tr("最小板长(mm):		"));
	str = m_code->toUnicode(str);
	mixBoardLenButton ->setText(str);


}


int Setting::getNum()
{
	int res = 0;
	NumKeyBoard* keyBoard = new NumKeyBoard(this, "keyBoard", true, WStyle_Customize | WStyle_NoBorder);
	keyBoard->setFixedSize(240, 320);
	res = keyBoard->exec();
	assert(res != -1);
	delete keyBoard;
	return res;
	
}


int Setting::pipeLen() const
{
	return m_pipeLen;
}
int Setting::radius() const
{
	return m_radius;
}
int Setting::maxBoardLen() const
{
	return m_maxBoardLen;
}
int Setting::mixBoardLen() const
{
	return m_mixBoardLen;
}


